﻿using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.ShopCart
{
    public class AppCartHelper
    {
        private readonly IApp_cart_keysService _app_cart_keys;
        private readonly IArticleService _article;
        private readonly IUser_groupsService _user_groups;
        private readonly IArticle_goodsService _article_goods;
        public AppCartHelper(IApp_cart_keysService app_cart_keys, IArticleService article, IUser_groupsService user_groups, IArticle_goodsService article_goods)
        {
            this._app_cart_keys = app_cart_keys;
            this._article = article;
            this._user_groups = user_groups;
            this._article_goods = article_goods;
        }
        #region 基本增删改方法====================================
        /// <summary>
        /// 获得购物车列表
        /// </summary>
        public List<cart_items> GetList(int group_id,int user_id)
        {
            List<App_cart_keys> ls = GetCart(user_id); //获取购物车商品
            return ToList(ls, group_id);
        }
        /// <summary>
        /// 添加到购物车
        /// </summary>
        public bool Add(int article_id, int goods_id, int quantity, int shop_id,int user_id)
        {
            List<App_cart_keys> ls = GetCart(user_id);
            if (ls != null)
            {
                App_cart_keys modelt = ls.Where(p => p.article_id == article_id && p.goods_id == goods_id).FirstOrDefault();
                if (modelt != null)
                {
                    modelt.quantity += quantity; //更新数量
                    _app_cart_keys.Update(modelt);
                    return true;
                }
            }
            else
            {
                //不存在的则新增
                ls.Add(new App_cart_keys() { article_id = article_id, goods_id = goods_id, quantity = quantity, shop_id = shop_id, user_id = user_id });
                _app_cart_keys.Add(ls[0]);
            }            
            return true;
        }
        /// <summary>
        /// 更新购物车数量
        /// </summary>
        public App_cart_keys Update(int article_id, int goods_id, int quantity,int user_id)
        {
            //如果数量小于1则移除该项
            if (quantity < 1)
            {
                return null;
            }
            List<App_cart_keys> ls = GetCart(user_id);
            if (ls != null)
            {
                App_cart_keys modelt = ls.Where(p => p.article_id == article_id && p.goods_id == goods_id).FirstOrDefault();
                if (modelt != null)
                {
                    int i = ls.FindIndex(p => p.article_id == article_id && p.goods_id == goods_id);
                    modelt.quantity = quantity; //更新数量
                    _app_cart_keys.Update(modelt);
                    return modelt;
                }
            }
            return null;
        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        public void Clear(int user_id)
        {
            _app_cart_keys.Remove(user_id);
        }

        /// <summary>
        /// 移除购物车指定项
        /// </summary>
        public void Clear(int article_id, int goods_id,int user_id)
        {
            if (article_id > 0)
            {
                List<App_cart_keys> cartList = GetCart(user_id);
                if (cartList == null)
                {
                    return;
                }
                App_cart_keys modelt = cartList.Where(p => p.article_id == article_id && p.goods_id == goods_id).FirstOrDefault();
                if (modelt != null)
                {
                    cartList.Remove(modelt); //移除指定的项
                    _app_cart_keys.Delete(modelt.id);
                }
            }
        }

        /// <summary>
        /// 移除购物车指定项
        /// </summary>
        public void Clear(List<App_cart_keys> ls,int user_id)
        {
            if (ls != null)
            {
                List<App_cart_keys> cartList = GetCart(user_id);
                if (cartList == null)
                {
                    return;
                }
                foreach (App_cart_keys modelt in ls)
                {
                    App_cart_keys model = cartList.Where(p => p.article_id == modelt.article_id && p.goods_id == modelt.goods_id).FirstOrDefault();
                    if (model != null)
                    {
                        cartList.Remove(model);
                        _app_cart_keys.Delete(model.id);
                    }
                }
            }
        }
        #endregion
        #region 扩展方法==========================================
        /// <summary>
        /// 转换成List
        /// </summary>
        public List<cart_items> ToList(List<App_cart_keys> ls, int group_id)
        {
            if (ls != null)
            {
                List<cart_items> iList = new List<cart_items>();
                foreach (App_cart_keys item in ls)
                {
                    //查询文章的信息
                    Article articleModel = _article.GetModel(item.article_id);
                    if (articleModel == null)
                    {
                        continue;
                    }
                    //查询商品信息
                    Article_goods goodsModel = articleModel.goods.Where(p => p.id == item.goods_id).FirstOrDefault();
                    if (articleModel.goods != null && articleModel.goods.Count > 0 && goodsModel == null)
                    {
                        continue;
                    }
                    //开始赋值
                    cart_items modelt = new cart_items();
                    modelt.channel_id = articleModel.channel_id;
                    modelt.shop_id = articleModel.shop_id;
                    modelt.article_id = articleModel.id;
                    modelt.goods_no = articleModel.goods_no;
                    modelt.title = articleModel.title;
                    modelt.img_url = articleModel.img_url;
                    modelt.sell_price = articleModel.sell_price;
                    modelt.pv_price = articleModel.pv_price;
                    modelt.user_price = articleModel.sell_price;
                    modelt.user_pv_price = articleModel.pv_price;
                    modelt.point = articleModel.point;
                    modelt.stock_quantity = articleModel.stock_quantity;
                    bool setStatus = false; //会员组价格赋值状态
                    if (goodsModel != null)
                    {
                        modelt.stock_quantity = goodsModel.stock_quantity; //库存
                        modelt.sell_price = goodsModel.sell_price; //商品价格
                        modelt.pv_price = goodsModel.pv_price;
                        modelt.user_price = goodsModel.sell_price; //会员组价格
                        modelt.user_pv_price = goodsModel.pv_price;
                        modelt.goods_id = goodsModel.id;
                        modelt.goods_no = goodsModel.goods_no;
                        modelt.spec_text = goodsModel.spec_text;
                        //会员组价格
                        if (group_id > 0 && goodsModel.group_prices != null)
                        {
                            User_group_price userPriceModel = goodsModel.group_prices.Find(p => p.group_id == group_id);
                            if (userPriceModel != null)
                            {
                                setStatus = true; //已赋值
                                modelt.user_price = userPriceModel.price;
                                modelt.user_pv_price = userPriceModel.pv_price;
                            }
                        }
                    }
                    //如果未曾有会员组价格则使用折扣价格
                    if (group_id > 0 && !setStatus)
                    {
                        int discount = _user_groups.GetDiscount(group_id);
                        if (discount > 0)
                        {
                            modelt.user_price = modelt.sell_price * discount / 100;
                            modelt.user_pv_price = modelt.pv_price * discount / 100;
                        }
                    }
                    modelt.quantity = item.quantity;
                    //添加入列表
                    iList.Add(modelt);
                }
                return iList;
            }
            return null;
        }

        /// <summary>
        /// 统计购物车
        /// </summary>
        public cart_total GetTotal(int group_id,int user_id)
        {
            List<cart_items> ls = GetList(group_id,user_id);
            return GetTotal(ls);
        }

        /// <summary>
        /// 统计购物车
        /// </summary>
        public cart_total GetTotal(List<cart_items> ls)
        {
            cart_total model = new cart_total();
            if (ls != null)
            {
                foreach (cart_items modelt in ls)
                {
                    model.total_num++;
                    model.total_quantity += modelt.quantity;
                    model.payable_amount += modelt.sell_price * modelt.quantity;
                    model.real_amount += modelt.user_price * modelt.quantity;
                    model.pv_amount += modelt.user_pv_price * modelt.quantity;
                    model.total_point += modelt.point * modelt.quantity;
                }
            }
            return model;
        }

        /// <summary>
        /// 只统计购物车数量
        /// </summary>
        /// <returns></returns>
        public int GetQuantityCount(int user_id)
        {
            int count = 0;
            List<App_cart_keys> ls = GetCart(user_id);
            if (ls != null)
            {
                foreach (cart_keys modelt in ls)
                {
                    bool isExists = false;
                    if (modelt.goods_id > 0)
                    {
                        isExists = _article_goods.Exists(modelt.article_id, modelt.goods_id);
                    }
                    else
                    {
                        isExists = _article.Exists(modelt.article_id);
                    }
                    if (isExists)
                    {
                        count += modelt.quantity;
                    }
                }
            }
            return count;
        }
        #endregion
        #region 私有方法==========================================
        /// <summary>
        /// 获取 数据库中购物内容
        /// </summary>
        private List<App_cart_keys> GetCart(int user_id)
        {
            List<App_cart_keys> ls = _app_cart_keys.GetList(user_id); //获取Cookies值
            return ls;
        }
        #endregion
    }
}
